/**
 * ©2013-2015 Alan L. Rights Reserved.
 */
package org.freedom.railhunter.service;

import java.io.IOException;
import java.util.Map;
import org.codehaus.jackson.JsonNode;
import org.freedom.railhunter.model.Passenger;
import org.freedom.railhunter.util.HttpConnectionManager;
import org.freedom.railhunter.config.PathConfig;
import org.freedom.railhunter.exception.CanNotBookingException;
import org.freedom.railhunter.exception.SessionExpiredException;
import org.freedom.railhunter.model.AcceptDataType;
import static org.freedom.railhunter.service.BaseService.DATA;
import static org.freedom.railhunter.service.BaseService.object_mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Alan
 */
public class PassengerService extends BaseService {
 
    private static final Logger logger = LoggerFactory.getLogger(PassengerService.class);
    
    public static final String NORMAL_PASSENGERS = "normal_passengers";
    
    public static final String NO_LOGIN = "noLogin";
    
    public static final String IS_EXIST = "isExist";
    
    public static final String EX_MSG = "exMsg";
    
    private static PassengerService  passengerService;
    
    private PassengerService() {
    }
    
    public static PassengerService getInstance() {
        if (passengerService == null) {
            passengerService = new PassengerService();
        }
        return passengerService;
    }

     /**
     * 获取常用联系人
     * 
     * @param cookieData 
     */
    public Passenger[] getNoramlPassengers(Map<String, String> cookieData) throws SessionExpiredException, CanNotBookingException {
        try {
            String result = HttpConnectionManager.postHttpRequestAsString(PathConfig.GET_PASSENGER_DTOS_PATH,
                    PathConfig.PASSENGERS_INIT_PATH, AcceptDataType.All, null, cookieData);
            JsonNode root = object_mapper.readTree(json_factory.createJsonParser(result));
            if (root != null) {
                JsonNode data = root.get(DATA);
                if (data != null) {
                    // 是否登录
                    JsonNode noLoginNode = data.get(NO_LOGIN);
                    if (noLoginNode != null && noLoginNode.getBooleanValue()) {
                        throw new SessionExpiredException("您需要重新登录！");
                    }
                    
                    // 是否存在数据（有可能不能预订）
                    JsonNode isExistNode = data.get(IS_EXIST);
                    JsonNode exMsgNode = data.get(EX_MSG);
                    if (isExistNode != null && !isExistNode.getBooleanValue()) {
                        throw new CanNotBookingException(exMsgNode.asText());
                    }
                    
                    JsonNode normalPassengers = data.get(NORMAL_PASSENGERS);
                    return object_mapper.readValue(normalPassengers, Passenger[].class);
                }
            }
        } catch (IOException ex) {
            logger.error("获取常用联系人异常", ex);
        }
        return new Passenger[]{};
    }
}
